package com.hskn.hss.module.userdepart.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.instorageCfg.mapper.TlInStorageCfgMapper;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.module.userdepart.service.ISysUserDepartService;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Slf4j
@RestController
@RequestMapping("/hss/sysuserdepart")
public class SysUserDepartController {

    @Autowired
    ISysUserDepartService sysUserDepartService;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_user_depart", bean = SysUserDepart.class, beanMapper = SysUserDepartMapper.class, names = "depid, userid")
    @RequestMapping(value = "insertUserDepart", method = RequestMethod.POST)
    public AjaxResult<Object> insertUserDepart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysUserDepartService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_user_depart", idKey = "ID", bean = SysUserDepart.class, beanMapper = SysUserDepartMapper.class, names = "depid, userid")
    @RequestMapping(value = "updateUserDepart", method = RequestMethod.POST)
    public AjaxResult<Object> updateUserDepart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysUserDepartService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_user_depart", idKey = "ID", bean = SysUserDepart.class, beanMapper = SysUserDepartMapper.class, names = "depid, userid")
    @RequestMapping(value = "deleteUserDepartById", method = RequestMethod.POST)
    public AjaxResult<Object> deleteUserDepartById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysUserDepartService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getUserDepartById", method = RequestMethod.POST)
    public AjaxResult<Object> getUserDepartById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysUserDepart SysUserDepart = null;
        try {
            SysUserDepart = sysUserDepartService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(SysUserDepart);

    }

    @RequestMapping(value = "getUserDepartList", method = RequestMethod.POST)
    public AjaxResult<Object> getUserDepartList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        List<SysUserDepart> userDepartsList = null;
        try {
            userDepartsList = sysUserDepartService.getListByMap(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(userDepartsList);
    }
}

